﻿' TODO: replace these with the processor input and output types.
Imports TInput = System.String
Imports TOutput = System.String
Imports System.IO
Imports System.Text
Imports System.Windows.Forms
Imports Microsoft.Xna.Framework.Content.Pipeline.Graphics
Imports Microsoft.Xna.Framework.Content.Pipeline.Processors
Imports System.Threading.Tasks
Imports System.Threading

''' <summary>
''' This class will be instantiated by the XNA Framework Content Pipeline
''' to apply custom processing to content data, converting an object of
''' type TInput to TOutput. The input and output types may be the same if
''' the processor wishes to alter data without changing its type.
'''
''' This should be part of a Content Pipeline Extension Library project.
'''
''' TODO: change the ContentProcessor attribute to specify the correct
''' display name for this processor.
''' </summary>
<Pipeline.ContentProcessorAttribute(DisplayName:="FontProcessor.ChineseFontProcessor")>
Public Class FontProcessor
    Inherits FontDescriptionProcessor

    Public Shared Property Cancel() As Boolean = False

    Public Iterator Function Search(ByVal path As String, ByVal filter As String) As IEnumerable(Of String)
        Dim di As New DirectoryInfo(path)
        For Each fi In di.GetFiles(filter)
            Yield fi.FullName
        Next
        For Each d In di.GetDirectories()
            For Each item In Search(d.FullName, filter)
                Yield item
            Next
        Next
    End Function

    Public Overrides Function Process(input As Microsoft.Xna.Framework.Content.Pipeline.Graphics.FontDescription, context As Microsoft.Xna.Framework.Content.Pipeline.ContentProcessorContext) As Microsoft.Xna.Framework.Content.Pipeline.Processors.SpriteFontContent
        'Dim dlg As New FormProgress(input)
        'Dim worker As New Thread(
        '    Sub()
        '        dlg.ShowDialog()
        '    End Sub)
        'worker.Start()
        'worker.Join()
        'MessageBox.Show("Characters have been added.")
        'Dim worker As New Thread(
        '    Sub()
        '        input.DefaultCharacter = "?"c
        '        Using ofd As New OpenFileDialog()
        '            With ofd
        '                .Title = "Open text files"
        '                .Filter = "Text files(*.*)|*.txt|All files(*.*)|*.*"
        '                .Multiselect = True
        '                If .ShowDialog() = DialogResult.OK Then
        '                    For Each item In .FileNames
        '                        Dim fi As New FileInfo(item)
        '                        If fi.Exists Then
        '                            Dim txt = fi.OpenText().ReadToEnd()
        '                            Dim chars = txt.ToCharArray()
        '                            For Each c In chars
        '                                If input.Characters.Contains(c) = False Then
        '                                    input.Characters.Add(c)
        '                                End If
        '                            Next
        '                        End If
        '                    Next
        '                End If
        '            End With
        '        End Using
        '    End Sub)
        'worker.Start()
        'worker.Join()
        'MessageBox.Show(My.Application.Info.DirectoryPath)
        On Error Resume Next
        Dim di As New DirectoryInfo(My.Application.Info.DirectoryPath)
        Dim doAdd =
            Sub(path As String, filter As String)
                For Each fi In Search(path, filter).Select(Function(item) New FileInfo(item))
                    If fi.Exists Then
                        Using ss = fi.OpenRead()
                            Using tr As TextReader = New StreamReader(ss, Encoding.Default)
                                Dim txt = tr.ReadToEnd()
                                Dim chars = txt.ToCharArray()
                                For Each c In chars
                                    If input.Characters.Contains(c) = False Then
                                        input.Characters.Add(c)
                                    End If
                                Next
                            End Using
                        End Using
                    End If
                Next
            End Sub

        Dim paths = {"F:\The Liars",
                     "F:\Programming\Runa\Sources\Pure2DTrunk\Sources\Runa Studio\ContentBuilder\FontProcessor\bin\x86\Release\最后需要生成字体的所有内容"}
        For Each p In paths
            doAdd(p, "*.yml")
            doAdd(p, "*.txt")
        Next

        'MessageBox.Show(input.Characters.Count.ToString())
        Return MyBase.Process(input, context)
    End Function

End Class
